package com.magicode.cms.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.ICommentDao;
import com.magicode.cms.entiy.base.NewsComment;
import com.magicode.cms.service.ICommentService;


@Service("servicecomment")
public class CommentServiceImpl extends BaseServiceImpl<NewsComment, ICommentDao> implements ICommentService,UtilPath{

	//****************************************************************************
	@Autowired
	@Qualifier("daocomment")
	private ICommentDao commentDao;
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "news");
		map.put(json_title, "新闻标题");
		map.put("width", 160);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "username");
		map.put(json_title, "用户名");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "textString");
		map.put(json_title, "内容");
		map.put("width", 195);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "创建时间");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "count");
		map.put(json_title, "追加点评");
		map.put("width", 70);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览追加");
		opt.setUrl(getCtx().getContextPath()+"/comment/reply?rid="+id+"");
		opt.setType("preview");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/comment/"+id+"");
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}
	//*************************************************************************
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql =" from "+NewsComment.class.getName()+" "+entity_alias_name+" ";
		hql += " left join "+entity_alias_name+".news as news ";
		hql += " left join "+entity_alias_name+".author as au ";
		hql += " left join "+entity_alias_name+".replyComment as comment ";
		if(!map.isEmpty()){
			if(map.containsKey("news")){
				hql = hql+" where "+entity_alias_name+".news.id='"+map.get("news")+"'";
			}
			if(map.containsKey(field_id)){
				hql = hql+ this.getWhereOrAnd(hql)+ "  "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" where "+entity_alias_name+".news.title like '%"+map.get("title")+"%'";
			}
			if(map.containsKey("userName")){
				hql = hql+" where "+entity_alias_name+".author.userName like'%"+map.get("userName")+"%'";
			}
			if(map.containsKey("rid")){
				hql = hql+" where "+entity_alias_name+".replyComment.id='"+map.get("rid")+"'";
			}
		}
		return hql;
	}
	
	//****************************************************************************
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		List<Map<String, Object>> resultList = null;
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".text as textString");
			gridsel.put("newsTitle", "news.title as news");
			gridsel.put("username", "au.userName as author");
			gridsel.put("guestName", ""+entity_alias_name+".guestName as guestName");
			gridsel.put("createTime", ""+entity_alias_name+".createTime as createTime");
			gridsel.put("count", " ( select count(r.id) from "+NewsComment.class.getName()+" r where r.replyComment = t ) as count " );
//					"size("+entity_alias_name+".relateReplyCommonts) as count");
			
			resultList = this.queryMapDataList(hql, gridsel, start, size);
			//将List中Map的byte[]转化为String
			resultList = super.convertByteToString(resultList,"textString");
			List<Map<String, Object>> list = null;
			for (Map<String, Object> map : resultList) {
				String author=(String) map.get("author");
				String guestName=(String) map.get("guestName");
				if(guestName==null){
					map.put("username", author);
				}else {
					map.put("username", guestName);
				}
			}
			return resultList;
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	//*********************************************************************************
	@Override
	public ICommentDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return commentDao;
	}
	
	public ICommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(ICommentDao commentDao) {
		this.commentDao = commentDao;
	}
}
